package demo3;

import demo2.IndexException;

public class MyLinkedList {

    static class ListNode {
        int val;
        ListNode prev;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    //头插
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = last = node;
            return;
        }

        node.next = head;
        head.prev = node;
        head = node;
    }

    //尾插
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = last = node;
            return;
        }

        node.prev = last;
        last.next = node;
        last = node;
    }

    //计算结点总数
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    private void checkIndexInAdd(int index) throws IndexException {
        if(index < 0 || index > size()) {
            throw new IndexException("下标越界！");
        }
    }

    //指定位置插入
    public void addIndex(int index, int data) {
        try {
            checkIndexInAdd(index);
            if(index == 0) {
                addFirst(data);
                return;
            }

            if(index == size()) {
                addLast(data);
                return;
            }

            ListNode cur = head;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            ListNode node = new ListNode(data);
            node.prev = cur.prev;
            node.next = cur;
            cur.prev.next = node;
            cur.prev = node;
        } catch (IndexException e) {
            e.printStackTrace();
        }
    }

    //删除某个元素
    public void remove(int key) {
        if(head == null) {
            return;
        }

        //头节点删除
        if(head.val == key) {
            head = head.next;
            if(head != null) {
                head.prev = null;
            }
            return;
        }

        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                cur.prev.next = cur.next;
                if(cur.next == null) { //后一个结点为null ，说明是尾删，要单独处理一下
                    last = cur.prev;
                } else {
                    cur.next.prev = cur.prev;
                }
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有的key结点
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }

        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                cur.prev.next = cur.next;
                if(cur.next == null) { //后一个结点为null ，说明是尾删，要单独处理一下
                    last = cur.prev;
                } else {
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }

        //头节点删除
        if(head.val == key) {
            head = head.next;
            if(head != null) {
                head.prev = null;
            }
        }
    }

    //是否包含某个元素
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur !=  null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //清空链表
    public void clear() {
        ListNode cur = head;
        while(cur !=null) {
            ListNode tmp = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = tmp;
        }
        head = last = null;
    }

    //打印链表
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
